home *** CD-ROM | disk | FTP | other *** search
- /* NAME
-
- yesorno -- Get a yes or no answer to a question
-
- SYNOPSIS
-
- answer = yesorno(question)
-
- int answer; TRUE if yes, FALSE if no
- char *question; Question to be asked. Ends
- with a question mark.
-
- DESCRIPTION
-
- yesorno is a Lattice C, Version 3.11 function. It writes an
- error message on the next line and re-requests input until it
- receives either a 'Y', a 'y', a 'N' or a 'n' answer.
- It uses IBM BIOS interrupt 0x16 for input and stderr for output,
- redirection does no affect it.
- By changing the test inside the parenthesis of VALID_ANS to
- another test for legal characters and the initialization of the static
- character array error_message to a different one-line message, you
- can use the same basic function for other tests.
-
- CAUTIONS
-
- As written, it uses the REGS union from Lattice's include file
- dos.h and the Lattice function int86, neither of which may be
- portable to another compiler.
-
- LIBRARIES USED
-
- None.
-
- AUTHOR: Lew Paper
- DATE WRITTEN :
-
- */
-
- #include <stdio.h>
- #include <dos.h>
- #include <string.h>
-
- #define VIDEO_FUNC(INREGS,OUTREGS) (void) int86(0x10, &INREGS, &OUTREGS)
- #define VIDEO_INT VIDEO_FUNC(in, out)
-
- #define VALID_ANS (answer == 'Y' || answer == 'N')
-
- yesorno(question)
- char *question;
-
- {
- static char error_message[] = "Y(es) or N(o) only please";
- static int error_length = 0; /* length of error_message - 1*/
- int ans_loc; /* location of answer */
- static int error_loc = 0; /* location of error message */
- int answer;
-
- union REGS in, out; /* Registers without segments */
-
- #ifdef DEBUG
- int i;
- #endif /* #ifdef DEBUG */
-
- if (!error_length) /* Use error_length for clearing */
- /* to fit all screen widths */
- error_length = strlen(error_message) - 1;
-
- /* Set BH of in to current active screen page. Note the reversal */
- /* of roles between in and out. */
- out.h.ah = 0x0f; /* Get current display mode */
- VIDEO_FUNC(out, in);
-
- fputs(question, stderr); /* Display question */
- fputc(' ', stderr); /* Add a space to separate answer */
-
- /* Set ans_loc to cursor column for answer */
- in.h.ah = 3; /* Get cursor location */
- VIDEO_INT; /* Sets DL to column */
- ans_loc = out.h.dl;
-
- answer = 0;
-
- do
- {
- /* Read answer */
- in.h.ah = 0x00; /* Read keyboard interrupt */
- int86(0x16, &in, &out);
- answer = toupper(out.h.al);
-
- fputc(answer, stderr);
- fputc('\n', stderr);
-
- /* Set error_loc to cursor row if necessary */
- if (!error_loc)
- {
- in.h.ah = 3; /* Get cursor location */
- VIDEO_INT; /* Sets DH to row */
- error_loc = (int) out.h.dh << 8;
-
- ans_loc |= (error_loc - 0x0100); /* Up one row from error */
-
- #ifdef DEBUG
- /* Fill all but the last position of error line with a row of asterisks */
- in.h.ah = 0x0f; /* Get current display mode */
- /* Set AH to number of columns */
- /* on screen */
- VIDEO_INT;
- for (i = (int) out.h.ah - 1; i; i--)
- fputc('*', stderr);
-
- /* Move cursor back to start of error line */
- in.h.ah = 2; /* Set cursor position */
- in.x.dx = error_loc;
- VIDEO_INT;
- #endif /* #ifdef DEBUG */
-
- }
-
- /* Clear old error message */
- in.x.ax = 0x0600; /* Blank window */
- in.x.cx = error_loc; /* Start of line as UL corner */
- in.x.dx = error_loc | error_length;
- /* End of message as LR corner */
- VIDEO_INT;
-
- if (!VALID_ANS)
- {
- fputs(error_message, stderr);
-
- /* Move cursor to ans_loc */
- in.h.ah = 02; /* Set cursor position */
- in.x.dx = ans_loc;
- VIDEO_INT;
- }
-
- } while (!VALID_ANS);
-
- return(answer == 'Y');
-
- }
-
- #ifdef DEBUG
- main(argc, argv)
- int argc;
- char *argv[];
-
- {
- union REGS min, mout;
- static int q_row = 0;
- int ans;
-
- mout.h.ah = 0x0f; /* Read display mode */
- VIDEO_FUNC(mout, min); /* Set active page */
-
- min.x.ax = 0x0600; /* Clear screen */
- min.x.cx = 0;
- min.x.dx = 0x2479;
- VIDEO_FUNC(min, mout);
-
- if (argc >= 3)
- q_row = (atoi(argv[2]) << 8);
- min.h.ah = 2; /* Set cursor position */
- min.x.dx = q_row;
- VIDEO_FUNC(min, mout);
-
- if (argc >= 2)
- ans = yesorno(argv[1]);
- else
- ans = yesorno("Do you want to end testing yesorno?");
- fputs((ans ? "Yes" : "No"), stderr);
-
- min.h.ah = 2; /* Set cursor position */
- min.x.dx = (q_row < (13 << 8) ? q_row + 0x0800 : q_row - 0x0800);
- VIDEO_FUNC(min, mout);
-
- if (argc == 1 || argc > 3)
- fputs("USAGE yesorno [question [row]] WHERE row IS 0-24\n", stderr);
-
- }
-
- #endif /* #ifdef DBUG */
-